// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()
const db = cloud.database()
const post = db.collection('post')
var $ = db.command.aggregate
// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()

  try {
  return await post.aggregate()
      .lookup({
        from: "user",
        localField: "_openid",
        foreignField: "_openid",
        as: "messageList"
      })
      .replaceRoot({
      newRoot: $.mergeObjects([ $.arrayElemAt(['$messageList', 0]), '$$ROOT' ])
    })
    .project({
      messageList: 0
    })
    
      .end()
      .then(res => 
        {console.log(res)
        return res
        }
          
      )
      .catch(err => console.error(err))

  } catch (error) {
    console.log(error)
  }

 // return {
 //   message,
  //  event,
   // openid: wxContext.OPENID,
   // appid: wxContext.APPID,
//unionid: wxContext.UNIONID,
 // }
}